package com.woniu.liuzhougov.itemQueryAndNotice.controller;

import com.woniu.liuzhougov.authorityControl.constant.UserConstant;
import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.pojo.CheckStatus;
import com.woniu.liuzhougov.itemAndTask.pojo.Item;
import com.woniu.liuzhougov.itemAndTask.pojo.Urgenry;
import com.woniu.liuzhougov.itemQueryAndNotice.service.SxService;
import com.woniu.liuzhougov.itemQueryAndNotice.vo.AreaStatistic;
import com.woniu.liuzhougov.itemQueryAndNotice.vo.DepartmentStatistic;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @version 1.0
 * @Author yangzhuolin
 * @ClassName SxController
 * @Description 处理与事项查询相关的请求
 * @Date 2019/4/23 14:46
 */
@Controller
@RequestMapping("sx")
public class SxController {

    @Autowired
    private SxService sxService;

     //根据条件查询事项
     @RequestMapping("/query")
     @ResponseBody
     public Map queryItems(CheckStatus checkStatus, User user, Urgenry urgency, Item item){
         List<Item> items = sxService.queryItems(checkStatus,user, urgency,item);
         Map<String,Object> map1 = new HashMap<>();
         map1.put("code",0);
         map1.put("count",items.size());
         map1.put("msg","");
         map1.put("data",items);
         return map1;
     }
     @RequestMapping("/cityQuery")
     @ResponseBody
     public Map cityQuery(CheckStatus checkStatus, User user, Urgenry urgency, Item item){
         List<Item> cityItems = sxService.cityQueryItems(checkStatus, user, urgency, item);
         Map<String,Object> map2 = new HashMap<>();
         map2.put("code",0);
         map2.put("count",cityItems.size());
         map2.put("msg","");
         map2.put("data",cityItems);
         return map2;
     }


    //查询事项的审批级别，并返回给前端下拉框
    @RequestMapping("/findUrgenry")
    @ResponseBody
    public  List<Urgenry> findUrgenry(){
        List<Urgenry> urgency = sxService.findUrgenry();
        //System.out.println(urgency);
        return urgency;
   }


    //查询事项审批状态，并返回给前端的下拉框。
   @RequestMapping("/findStatue")
   @ResponseBody
   public  List<CheckStatus> findStatue(){

       List<CheckStatus> statue = sxService.findStatue();
       return statue;
   }

   /*
      按区域查询事项个数
    */
    @RequestMapping("/queryCountByArea")
    @ResponseBody
    public List<AreaStatistic> queryCountByArea(){
        List<AreaStatistic> areaItemCount = sxService.queryCountByArea();
        return areaItemCount;
    }

    /*
      按部门查询事项个数
     */
    @RequestMapping("/queryCountByDepartment")
    @ResponseBody
    public List<DepartmentStatistic>  queryCountByDepartment(){
        List<DepartmentStatistic> departmentItemCount = sxService.queryCountByDepartment();
        return departmentItemCount;
    }

    //通过查询事项，来查询事项发起人,并返回给前端下拉框。
    @RequestMapping("/findFounder")
    @ResponseBody
    public List<User> listUserByDepartmentId(){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute(UserConstant.CURRENT_USER);
        Integer departmentId = currentUser.getDepartment().getDepartmentId();
        List<User> users = sxService.listUserByDepartmentId(departmentId);
        return users;
    }
}
